#include <func.h>
#include "token.h"
#include "virtualFileSystem.h"
#include "head.h"
#include "account.h"
int firstConnect(int netFd,clientMsg_t *client,char *tokenSalt){
        client->netFd = netFd;
        //用户登录界面
        //服务端与客户端共存一个用户名
        char *userName = (char *)calloc(50,sizeof(char));
        loadSever(netFd,&userName);
        strcpy(client->userName,userName);
        //客户端与服务端共存一个路径地址
        //获取当前用户的根目录
        client->path[0] = getOriginId(userName);
        client->pathCount = 0;
        //接收客户端登录时间
        train_t t;
        bzero(&t,sizeof(t));
        recv(netFd,&t.dataLength,sizeof(t.dataLength),0);
        recv(netFd,t.buf,t.dataLength,0);
        //组合用户名以及登录时间来生成token
        strcat(t.buf,userName);
        //生成token密文
        char token[200] = {0};
        strcpy(token,crypt(t.buf,tokenSalt));
        //输出token密文用于测试对比
        //puts(token);
        //发送token密文发给客户端保存
        bzero(&t,sizeof(t));
        t.dataLength = strlen(token);
        strcpy(t.buf,token);
        //printf("send datalength = %d\n",t.dataLength);
        //printf("send t.buf = %s\n",t.buf);
        send(netFd,&t,sizeof(t.dataLength) + t.dataLength,0);
        free(userName);
}           


int shortCommond(int netFd,clientMsg_t *client,int epfd){
        int id = 0;
         char command_buf[1024]={0};
         int ret = recv(netFd,command_buf,sizeof(command_buf),MSG_NOSIGNAL);
         if(ret == 0){
            
            puts("客户端关闭，结束连接");
            epollDel(client->netFd,epfd);
             return 0;
            }
         int command = atoi(command_buf);

         if(command == 0){
             return 0;
         }
         printf("收到指令%d\n",command);

         char pathBuf[1024]={0};
         switch (command)
         {
         case 1:{
             func1_ls(netFd,client->path[client->pathCount]);
             break;
         }

         case 2:
            //待修改
             id = func2(netFd,client->path[client->pathCount]);
             client->pathCount++;
             client->path[client->pathCount] = id;
             break;

         case 3:
             getPwd(netFd,client->path,client->pathCount);
             //func3(netFd);
             break;

         case 4:
             //func4(netFd);
             break;

         case 5:
             //func5(netFd);
             break;

         case 6:
             func6(netFd,client->userName);
             break;

         case 7:
             func7(netFd);
             break;

         case 8:
             //func8();
             break;

         default:
             printf("error\n");
             break;
         }

}

int func1_ls(int netFd,int pre_id){
    //ls_c(netFd);
    lsSever(netFd,pre_id);
}
//待修改
int func2(int netFd,int pre_id){
    int dataLength;
    char fileName[50] = {0};
    //接收目录名
    recv(netFd,&dataLength,sizeof(dataLength),0);
    recv(netFd,fileName,dataLength,0);
    int id = cd(fileName,pre_id);
    printf("cd id = %d\n",id);
    return id;


   /*  recv(netFd,pathBuf,sizeof(pathBuf),0);
    puts(pathBuf);
    cd_c(pathBuf, netFd); */
}

int func3(int netFd){
    pwd(netFd);
}

int func4(int netFd){
    uploadSever(netFd,"shuai",3);
    //recvFile(netFd);
}

int func5(int netFd){
    downSever(netFd,3);
    // char buf[1024]={0};
    // bzero(buf,sizeof(buf));
    // recv(netFd,buf,sizeof(buf),0);
    // sendFile(buf,netFd);//传入文件名
}

/* int func6(int netFd){
    remove_net(netFd);
} */

int func7(int netFd){
    viewlog(netFd);
}
int func6(int netFd,char *in_name){
    char name[100]={0};
    strcpy(name,in_name);
    char filename[80];
    char buf[50] = {0};
    recv(netFd,filename,sizeof(filename),0);
    MYSQL *db = mysql_init(NULL);
    char * host = "localhost";
    char * user = "root";
    char * password = "123";
    char * database = "netDisk";
    MYSQL * ret = mysql_real_connect(db,host,user,password,database,0,NULL,0);
    if(ret != 0){
        printf("mysql connect successed\n");
    }
    else{
        printf("error: %s\n",mysql_error(db));
        exit(-1);
    }
    char sql[300]={0};
    puts("1");
    sprintf(sql,"select id from fileDir where name = '%s' and user = '%s'",filename,name);
    mysql_query(db,sql);
    MYSQL_RES *result = mysql_store_result(db);
    MYSQL_ROW row;
    if(mysql_num_rows(result) == 0){
        strcat(buf,"文件不存在");
        puts(buf);
        send(netFd,buf,sizeof(buf),0);
    }
    else{
        sprintf(sql,"update fileDir set isdelete = 1 where name = '%s' and user = '%s'",filename,name);
        mysql_query(db,sql);
        strcat(buf,"删除成功");
        puts(buf);
        send(netFd,buf,sizeof(buf),0);
        bzero(sql,sizeof(sql));
        sprintf(sql,"select user from fileDir where name = '%s' and isdelete = 0",filename);
        mysql_query(db,sql);
        MYSQL_RES *result1 = mysql_store_result(db);
        if(mysql_num_rows(result1) == 0){
            printf("该文件没有用户使用，可以删除！\n");
            puts("1");
            remove_net(netFd,filename);
            bzero(sql,sizeof(sql));
            sprintf(sql,"delete from fileDir where file = '%s'",filename);
        }
        else{
            printf("该文件还有人使用，忽略实际删除\n");
        }
    }
    mysql_close(db);
    savelog(__FUNCTION__);
}


